package com.easycarebn.main.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.timbacsi.main.R;

public class ViewmoreDetailt {
    int mNum;
    ListView listView;
    private View view;
    private WebView wbContent;
    private Activity mActivity;
    private String mString;
    private ProgressBar loadding;

    public ViewmoreDetailt(Activity activity, String string) {
        mActivity = activity;
        mString = string;
    }

    public void getDialogReadMore() {
        final Dialog dialog = new Dialog(mActivity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.white);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(R.layout.dialogreadmore);
        loadding = (ProgressBar) dialog.findViewById(R.id.loadding);

        DisplayMetrics displaymetrics = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        TextView btnDatkhamItem = (TextView) dialog.findViewById(R.id.btnDatkhamItem);
        wbContent = (WebView) dialog.findViewById(R.id.loadcontent);
        settingWebview(wbContent);

        wbContent.loadDataWithBaseURL("http://www.wc.vtc.vn/", getHtmlLink(mString), "text/html", "utf-8", null);

        btnDatkhamItem.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

    }

    private void settingWebview(WebView webview_fbview) {
        webview_fbview.getSettings().setJavaScriptEnabled(true);
        webview_fbview.setLongClickable(false);

        webview_fbview.getSettings().setLoadWithOverviewMode(true);
        webview_fbview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        webview_fbview.getSettings().setUseWideViewPort(true);
        webview_fbview.requestFocus(View.FOCUS_DOWN);
        webview_fbview.setPadding(0, 0, 0, 0);
        webview_fbview.setWebViewClient(new webViewClient());
        webview_fbview.setWebChromeClient(new webChromeClient());
        webview_fbview.setInitialScale(100);
        webview_fbview.clearCache(true);
        webview_fbview.clearHistory();
        webview_fbview.getSettings().setDefaultFontSize(18);
        webview_fbview.addJavascriptInterface(new JavaScriptInterface(mActivity), "Android");
        webview_fbview.setVisibility(View.VISIBLE);

        webview_fbview.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                if (progress < 100 && loadding.getVisibility() == ProgressBar.GONE) {
                    loadding.setVisibility(ProgressBar.VISIBLE);
                }
                // Pbar.setProgress(progress);
                if (progress == 100) {
                    loadding.setVisibility(ProgressBar.GONE);
                }
            }
        });
    }

    public class JavaScriptInterface {
        Context mContext;

        // Instantiate the interface and set the context
        JavaScriptInterface(Context c) {
            mContext = c;
        }

        // using Javascript to call the finish activity
        public void closeMyActivity() {
            // finish();
        }
    }

    private String getHtmlLink(String content) {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int height = displaymetrics.heightPixels;
        int width = (displaymetrics.widthPixels-20);
/*
        String linkhtml = "<html>" + "<meta name='viewport',content='width=device-width, initial-scale=1, maximum-scale=1'>"
                + "<head>" + "<style type='text/css'>"
                + "img { max-width:"+width+"px!important; width: 100%%!important; height: auto!important; }"
                + "body{ font-size: 62.5%;}"
                + "p {  max-width: "+width+"px!important; width: 95%%!important;text-align: justify;  font-size:1.4em!important; line-height:50px;background:#ffffff;}"
                + "h1,h2,h3,h4{ font-weight: normarl!important;}" + "@font-face {" + "font-family: MyFont;"
                + "src: url('file:///android_asset/fonts/Roboto-Light.ttf')}"

                + " </style>" + "</head>"

                + "<body style='margin:0 ; padding: 0;  font-family: MyFont!important;'>"
                + "<div style='padding:20px!important;  margin: 0 auto;  '>" + content + "</div>" + "</body></html>";
        Log.d("linkhtml", linkhtml);*/
        
        return content;
    }

    private class webChromeClient extends WebChromeClient {

        // display alert message in Web View
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            new android.app.AlertDialog.Builder(view.getContext()).setMessage(message).setCancelable(true).show();
            result.confirm();
            return true;
        }
    }

    private class webViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // loadding.setVisibility(View.GONE);
            wbContent.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            int pos = url.indexOf("code=");
            loadding.setVisibility(View.GONE);

            super.onPageFinished(view, url);
        }
    }

}
